_ = require 'underscore'
Commodity = require('./mongoose_client').Commodity



noop = () ->

commodityHandle = 
  save : (data, cbf) ->
    cbf = cbf || noop
    if verify data, cbf
      new Commodity(data).save (err) ->
        cbf err
  count : (query, cbf) ->
    cbf = cbf || noop
    Commodity.count query, cbf
  findAll : (cbf) ->
    cbf = cbf || noop
    Commodity.find {sort : '_id'}, cbf
  find  : (query, options, cbf) ->
    cbf = cbf || noop
    Commodity.find query, null, options, cbf
  findByIdAndUpdate : (id, update, cbf) ->
    cbf = cbf || noop
    Commodity.findByIdAndUpdate id, update, cbf
    # Commodity.findById id, cbf

verify = (data, cbf) ->
  if _.isObject(data)
    return true
  else
    cbf new Error 'the save data is not a objcet' 
    return false

module.exports = commodityHandle


